import {
  FETCH_SCREENINFO,
  FETCH_SCREENLIST,
  ADD_SCREEN,
  DEL_SCREENINFO,
  SAVE_LAYOUTS,
  ADD_LAYOUT_PROGRAM
} from '../mutation-types'
import Service from '../../axios'
import openMessageWithType from './utils/message'

const state = {
  screenInfo: {},
  screenList: [],
  layouts: [] // 所有新增屏幕
}

const getters = {
  screenInfo: state => state.screenInfo,
  screenList: state => state.screenList,
  layouts: state => state.layouts
}

const actions = {
  getScreenInfo ({ commit }, screen) {
    Service.get('/screen/info?screen=' + screen)
      .then(res => {
        const success = Object.is(res.statusText, 'OK') && Object.is(res.data.ec, 0)
        if (success) commit('FETCH_SCREENINFO', res.data.data)
      })
  },
  getScreenList ({ commit }) {
    Service.get('/screen/list')
      .then(res => {
        const success = Object.is(res.statusText, 'OK') && Object.is(res.data.ec, 0)
        if (success) commit('FETCH_SCREENLIST', res.data.data)
      })
  },
  addScreen ({ commit }) {
    if (state.layouts.length > 0) {
      var data = []
      state.layouts.forEach(item => {
        data.push({
          screen: item.screen,
          line: item.line,
          row: item.row,
          grid: item.grid
        })
      })

      var result = true
      var errmsg = ''
      data.forEach(item => {
        Service.post('/screen/add', item)
          .then(res => {
            let success = Object.is(res.statusText, 'OK') && Object.is(res.data.ec, 0)
            if (!success) {
              errmsg += res.data.em + ';'
            }
            result = result && success
          })
      })
      if (result) {
        openMessageWithType('success', '', '所有新增屏幕已保存')
        // commit('ADD_SCREEN', data)
      } else {
        openMessageWithType('warning', '', '保存屏幕失败:' + errmsg)
      }
    }
  },
  delScreen ({ commit }, screen) {
    Service.get('/screen/del?screen=' + screen)
      .then(res => {
        const success = Object.is(res.statusText, 'OK') && Object.is(res.data.ec, 0)
        if (success) {
          commit('DEL_SCREENINFO', screen)
        }
      })
  },
  addLayoutProgram ({commit}, data) {
    commit('ADD_LAYOUT_PROGRAM', data)
  },
  // 保存所有屏幕的布局
  saveLayouts ({commit}, data) {
    commit('SAVE_LAYOUTS', data)
  }
}

const mutations = {
  [FETCH_SCREENINFO] (state, data) {
    state.screenInfo = data
  },
  [FETCH_SCREENLIST] (state, data) {
    state.screenList = data
  },
  [ADD_SCREEN] (state, data) {
    /*
    let ll = state.layouts.length
    // let sl = state.screenList.length
    state.layouts.splice(0, ll)

    data.forEach(item => {
      state.screenList.push(item.screen)
    })
    */
  },
  [DEL_SCREENINFO] (state, data) {
    state.screenList.filter(function (n, i) {
      if (n === data) {
        state.screenList.splice(i, 1)
      }
    })
  },
  [ADD_LAYOUT_PROGRAM] (state, data) {
    state.layouts[data.layIndex].grid[data.gridIndex].program = data.program
  },
  [SAVE_LAYOUTS] (state, data) {
    state.layouts.push(data)
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
